﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace LittleBee.CellStore.UserControls
{
	public partial class UCCellLibrary : UserControl
	{
		public UCCellLibrary()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			OpenFileDialog fd = new OpenFileDialog();

			if (fd.ShowDialog() == DialogResult.OK)
			{
				try
				{
					StoreCell cell = (Tag as StoreCell)!;

					Assembly lib = Assembly.LoadFrom(fd.FileName);
					string strFullCellType = "LittleBee.Cells." + cell.CellType;
					bool blFound = false;
					foreach (Type t in lib.GetExportedTypes())
					{
						if (t.FullName == strFullCellType)
						{
							blFound = true;
							break;
						}
					}

					if (blFound == false)
					{
						MessageBox.Show("Cell Type Mismatched", "Selete Library", MessageBoxButtons.OK);
					}
					else
					{
						cell.Library = Utils.Utils.EncodeBase64(File.ReadAllBytes(fd.FileName));
						cell.CodeOrLibrary = "library";
						cell.Save();
					}
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
				}
			}
		}
	}
}
